//
//  JCWFinancialProductCell.swift
//  HengYouCai
//
//  Created by wll on 2017/11/9.
//  Copyright © 2017年 lwj. All rights reserved.
//  理财产品单元格--代码

import UIKit

class JCWFinancialProductCell: UITableViewCell {
    
    
    ///配置cell
    override func wl_configurationCell(_ modelTemp: Any?, _ listView: UIScrollView, _ indexPath: IndexPath) {
        if let dataArr = modelTemp as? [ProjectModel] {
            let model = dataArr[indexPath.row]
            
            
            if model.cashRateShow == "1"{
                
                self.minProfitLab.attributedText = handlerProfit(model.profit, model.cashRateProfit)
            }else{
                
                self.minProfitLab.attributedText = handlerProfit(model.profit, model.profitFloat)
            }
                
                
//            self.minProfitLab.attributedText = handlerProfit(model.profit, model.profitFloat)
//            self.plstimeLimitValueLab.text = model.plstimeLimitType == "0" ?"\(model.plstimeLimitValue ?? "")天" :"\(model.plstimeLimitValue ?? "")个月"
            if(model.plstimeLimitType == "0"){
                 self.plstimeLimitValueLab.attributedText = handlerRichText([model.plstimeLimitValue,"天"], [Normal_333Color,Normal_333Color], [UIFont.systemFont(ofSize: 20),UIFont.systemFont(ofSize: 12)])
            }else{
                 self.plstimeLimitValueLab.attributedText = handlerRichText([model.plstimeLimitValue,"个月"], [Normal_333Color,Normal_333Color], [UIFont.systemFont(ofSize: 20),UIFont.systemFont(ofSize: 12)])
            }
           

            self.progerssView.setProgress(model.investPercent.rawFloat, animated: false)
            self.progressLab.text =  String(format: "%.1f", model.investPercent.rawFloat * 100) + "%"    //"\(model.investPercent.rawFloat * 100)" + "%"
            self.sminProfit.text = model.profitLabel
            self.titleLab.text = model.title
            self.tagView.tagsStr(model.tags)
            self.productStatusImageV.image = self.handleStatusImageV(JCWProductStatus(rawValue: model.status), model)
            pplstimeLimitValue.text = model.plstimeLimitLabel
            psurplusAmount.text = model.surplusAmountLabel
        }
    }
    
    //MARK:卡顿方法不用理会 为了优化理财列表滑动
    ///卡顿处理加载
    func kaDunHandler(_ modelTemp: Any?, _ listView: UITableView, _ indexPath: IndexPath) {//主要是这三个造成卡顿
        if let dataArr = modelTemp as? [ProjectModel] {
            let model = dataArr[indexPath.row]
            
            sminProfit.text = model.profitLabel
            pplstimeLimitValue.text = model.plstimeLimitLabel
            psurplusAmount.text = model.surplusAmountLabel
            
            self.titleLab.text = model.title
            self.tagView.tagsStr(model.tags)
            self.productStatusImageV.image = self.handleStatusImageV(JCWProductStatus(rawValue: model.status), model)
            self.progerssView.setProgress(model.investPercent.rawFloat, animated: false)
           self.progressLab.text =  String(format: "%.1f", model.investPercent.rawFloat * 100) + "%"

        }
    }
    ///卡顿剩余部分处理
    func kaDunHandler2(_ modelTemp: Any?, _ listView: UITableView, _ indexPath: IndexPath) {
        if let dataArr = modelTemp as? [ProjectModel] {
            let model = dataArr[indexPath.row]
            
            sminProfit.text = model.profitLabel
            pplstimeLimitValue.text = model.plstimeLimitLabel
            psurplusAmount.text = model.surplusAmountLabel
            
            
            if model.cashRateShow == "1"{
                
                self.minProfitLab.attributedText = handlerProfit(model.profit, model.cashRateProfit)
            }else{
                
                self.minProfitLab.attributedText = handlerProfit(model.profit, model.profitFloat)
            }
            if(model.plstimeLimitType == "0"){
                self.plstimeLimitValueLab.attributedText = handlerRichText([model.plstimeLimitValue,"天"], [Normal_333Color,Normal_333Color], [UIFont.systemFont(ofSize: 20),UIFont.systemFont(ofSize: 12)])
            }else{
                self.plstimeLimitValueLab.attributedText = handlerRichText([model.plstimeLimitValue,"个月"], [Normal_333Color,Normal_333Color], [UIFont.systemFont(ofSize: 20),UIFont.systemFont(ofSize: 12)])
            }
            
            self.progerssView.setProgress(model.investPercent.rawFloat, animated: false)
           self.progressLab.text =  String(format: "%.1f", model.investPercent.rawFloat * 100) + "%"
        }
    }
    
    
    
    ///处理状态图片 顺带处理剩余可投：募集总额：
    private func handleStatusImageV(_ status: JCWProductStatus?, _ model: ProjectModel) -> UIImage?{
        self.surplusAmountLab.text = NSString(money: model.surplusAmount) as String

        self.psurplusAmount.text = model.surplusAmountLabel
        self.progerssView.isHidden = false
        self.progressLab.isHidden = false
        if status == .saling {
            return nil
        }else if status == .prepareSale {
            self.progerssView.isHidden = true
            self.progressLab.isHidden = true
            self.psurplusAmount.text = model.surplusAmountLabel
            self.surplusAmountLab.text = NSString(money: model.productsScale) as String
            return UIImage(named: "advance")
        }else {
            self.psurplusAmount.text = model.surplusAmountLabel
            self.surplusAmountLab.text = NSString(money: model.productsScale) as String
            return UIImage(named: "repayment")
        }
    }
    
    ///处理利率 富文本
    private func handlerProfit(_ str1: String, _ str2: String) -> NSMutableAttributedString {
        
        let str11 = String(format: "%.2f", str1.cGFloat * 100)
        
        let temp = NSMutableAttributedString(string: str11, attributes: [NSFontAttributeName: Font_Auto(24)])
        temp.append(NSAttributedString(string: "%", attributes: [NSFontAttributeName: Font_Auto(16)]))
        if str2.isEmpty {
            return temp
        }else{
            let str22 = String(format: "%.2f", str2.cGFloat * 100)
            temp.append(NSAttributedString(string: "+\(str22)%", attributes: [NSFontAttributeName: Font_Auto(16)]))
            return temp
        }
    }
    
    ///快捷生成cell
    static func wl_createCellWithTableView(_ tableView: UITableView) -> JCWFinancialProductCell {
        
        let cellID =
            NSStringFromClass(JCWFinancialProductCell.self);
        if let cell = tableView.dequeueReusableCell(withIdentifier: cellID) as? JCWFinancialProductCell {
            return cell
        }else{
            return JCWFinancialProductCell(style: .default, reuseIdentifier: cellID)
        }
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.createContentView()
    }
    
    func createContentView() {
        self.selectionStyle = .none
        self.contentView.backgroundColor = Normal_458Color
        self.contentView.addSubview(self.cv)
        //产品标题title 年化收益率minProfit 投资期限plstimeLimitValue 剩余可投surplusAmount
        self.titleLab.setSingleLineAutoResizeWithMaxWidth(JCWSCREENW)
        self.cv.addSubview(self.titleLab)
        _ = self.titleLab
            .sd_layout()
            .topSpaceToView(self.cv,17.5)?
            .leftSpaceToView(self.cv,15)?
            .heightIs(14)
        
        self.cv.addSubview(self.tagView)
        _ = self.tagView
       .sd_layout()
        .centerYEqualToView(self.titleLab)?
        .leftSpaceToView(self.titleLab,5)
        //年化收益率minProfit 投资期限plstimeLimitValue 剩余可投surplusAmount
        self.minProfitLab.setSingleLineAutoResizeWithMaxWidth(150)
        self.cv.addSubview(self.minProfitLab)
        _ = self.minProfitLab
            .sd_layout()
            .topSpaceToView(self.titleLab,18)?
            .leftEqualToView(self.titleLab)?
            .heightIs(22)
        
        self.sminProfit.setSingleLineAutoResizeWithMaxWidth(100)
        self.cv.addSubview(self.sminProfit)
        _ = self.sminProfit
        .sd_layout()
        .topSpaceToView(self.minProfitLab,7)?
        .leftEqualToView(self.minProfitLab)?
        .heightIs(11)
        
        
        //投资期限plstimeLimitValue 剩余可投surplusAmount
        self.cv.addSubview(self.plstimeLimitValueLab)
        _ = self.plstimeLimitValueLab
            .sd_layout()
            .leftSpaceToView(self.cv,JCWSCREENW/2-30)?
            .centerYEqualToView(self.minProfitLab)?
            .widthIs(80)?
            .heightIs(22)
        
        self.pplstimeLimitValue.setSingleLineAutoResizeWithMaxWidth(100)
        self.cv.addSubview(self.pplstimeLimitValue)
        _ = self.pplstimeLimitValue
        .sd_layout()
        .centerXEqualToView(self.plstimeLimitValueLab)?
        .topSpaceToView(self.plstimeLimitValueLab,7)?
        .heightIs(13)
        
        // 剩余可投surplusAmount
        
        self.surplusAmountLab.setSingleLineAutoResizeWithMaxWidth(100)
        self.cv.addSubview(self.surplusAmountLab)
//        _ = self.surplusAmountLab
//            .sd_layout()
//            .rightSpaceToView(self.cv,15)?
//            .centerYEqualToView(self.pplstimeLimitValue)?
//            .heightIs(14)
        
       
        self.cv.addSubview(self.psurplusAmount)
//        _ = self.psurplusAmount.sd_layout()
//        .rightSpaceToView(self.surplusAmountLab,0)?
//        .centerYEqualToView(self.surplusAmountLab)?
//        .widthIs(80)?
//        .heightIs(14)
     
        
        //状态图
        self.cv.addSubview(self.productStatusImageV)
        _ = self.productStatusImageV
        .sd_layout()
        .widthIs(44)?
        .heightIs(44)?
        .rightSpaceToView(self.cv,0)?
        .topSpaceToView(self.cv,0)
        
        self.progerssView.center = CGPoint(x: self.cv.width_sd/3 * 2.25-8, y: 60)
        self.progerssView.bounds = CGRect(x: 0, y: 0, width: 65.aw_wl, height: 60)
        self.progerssView.transform = CGAffineTransform(scaleX: 1, y: 2)
        self.cv.addSubview(self.progerssView)
        
        
        self.progressLab.bounds = CGRect(x: 0, y: 0, width: 51, height: 14)
        self.progressLab.center = CGPoint(x: 0, y: 60)
        self.progressLab.left_sd = self.progerssView.right_sd
        self.cv.addSubview(self.progressLab)
        
        _ = self.psurplusAmount.sd_layout()?
            .leftSpaceToView(self.cv,self.cv.width_sd/3 * 2.25-65.aw_wl*0.5-8)?
            .centerYEqualToView(self.pplstimeLimitValue)?
            .heightIs(14)
        self.psurplusAmount.setSingleLineAutoResizeWithMaxWidth(100)
        _ = self.surplusAmountLab.sd_layout()?.leftSpaceToView(self.psurplusAmount,2)?.centerYEqualToView(self.pplstimeLimitValue)?.heightIs(14)
        self.surplusAmountLab.setSingleLineAutoResizeWithMaxWidth(100)
        //        _ = self.progerssView.sd_layout()?.widthIs(65.aw_wl)?.heightIs(60)?.topSpaceToView(self.cv,60)?.leftSpaceToView(self.psurplusAmount,-80)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    ///contentV
    let cv: UIView = {
        let temp = UIView(frame: CGRect(x: 0, y: 0, width: JCWSCREENW, height: 120))
        temp.backgroundColor = UIColor.white
        return temp
    }()
    
    ///产品标题title
    let titleLab: UILabel = {
        let temp = UILabel()
        temp.textColor = Normal_333Color
        temp.font = BoldFont_WL(14)
        temp.text = " "
        return temp
    }()
    
    ///标签 eg：双11 加息券活动
    let tagView: JCWActiveTagView = {
        let temp = JCWActiveTagView(ColorFromRGBHex_WL(0xece2cd),false, "")
        return temp
    }()
    
    ///年化收益率minProfit
    let minProfitLab: UILabel = {
        let temp = UILabel()
        temp.textColor = Normal_ThemeColor
        temp.font = BoldFont_WL(24)
        temp.text = " "
        return temp
    }()
    ///年化收益率minProfit
    let sminProfit: UILabel = {
        let temp = UILabel()
        temp.textColor = JCWColor_minProfit_gray
        temp.font = Font_WL(13)
//        temp.text = "历史年化收益率"
        return temp
    }()
    
    ///投资期限plstimeLimitValue
    let pplstimeLimitValue: UILabel = {
        let temp = UILabel()
        temp.textAlignment = .center
        temp.textColor = JCWColor_minProfit_gray
        temp.font = Font_Auto(13)
//        temp.text = "投资期限"
        return temp
    }()
    ///投资期限plstimeLimitValue
    let plstimeLimitValueLab: UILabel = {
        let temp = UILabel()
        temp.textAlignment = .center
        temp.textColor = ColorFromRGBHex_WL(0x333333)
        temp.font = Font_Auto(22)
        temp.text = "  "
        return temp
    }()
    ///剩余可投surplusAmount
    let psurplusAmount: UILabel = {
        let temp = UILabel()
        temp.textAlignment = .left
        temp.textColor = JCWColor_minProfit_gray
        temp.font = Font_Auto(13)
//        temp.text = "剩余可投："
        return temp
    }()
    ///剩余可投surplusAmount
    let surplusAmountLab: UILabel = {
        let temp = UILabel()
        temp.textColor = Normal_333Color
        temp.font = Font_Auto(13)
        temp.text = "  "
        return temp
    }()

    
    ///状态图eg：预售中 还款中
    let productStatusImageV: UIImageView = {
        let temp = UIImageView()
        return temp
    }()
    
    ///进度条
    lazy var progerssView: UIProgressView = {
        let temp = UIProgressView()
        temp.trackTintColor = Normal_458Color
        temp.progressTintColor = Normal_ThemeColor
        return temp
    }()
    ///进度文字
    lazy var progressLab: UILabel = {
        let temp = UILabel()
        temp.textColor = Normal_ThemeColor
        temp.font = Font_Auto(12)
        temp.textAlignment = .center
        temp.text = "   "
        return temp
    }()
    
    
}
